package ru.arriah.servicedesk.web.dispatcher.action;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.OrganizationBean;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestStatusBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;


public class RequestSearchFormAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;

   public RequestSearchFormAction() throws NamingException {
      super();
      requestManagerHome =
         Utils.getRequestManagerHomeInterface();
   }
   
   
   private RequestBean restoreBean(HttpServletRequest request) {
      RequestBean requestTemplate = new RequestBean();
      
      RequestStatusBean requestStatusBean = 
         new RequestStatusBean();
      
      requestStatusBean.setId(idVal(request.getParameter("status_query")));
      
      ClientBean clientBean = new ClientBean();
      clientBean.setFirstName(request.getParameter("client_query"));
      
      OrganizationBean organizationBean = new OrganizationBean();
      organizationBean.setFullTitle(request.getParameter("organization_query"));
      
      requestTemplate.setOrganization(organizationBean);
      requestTemplate.setClient(clientBean);
      requestTemplate.setRequestStatus(requestStatusBean);
      requestTemplate.setDispatcherText(request.getParameter("text_query"));
      requestTemplate.setClientText(request.getParameter("text_query"));
      requestTemplate.setId(idVal(request.getParameter("id_query")));
      requestTemplate.setRegNumber(request.getParameter("regnumber_query"));        
      
      DateFormat dateformat = 
          DateFormat.getDateInstance(
             DateFormat.SHORT, 
             new Locale("ru")
          );
      
      try {
         requestTemplate.setSearchToRegistrationDate(
            dateformat.parse(request.getParameter("toRegistrationDate"))
         );
      } catch (Exception e) {
         requestTemplate.setSearchToRegistrationDate(
            new Date()
         );
      }         
       
      try {
         requestTemplate.setSearchToEndDate(
            dateformat.parse(request.getParameter("toEndDate"))
         );
      } catch (Exception e) {
         requestTemplate.setSearchToEndDate(
            new Date()
         );
      }         
       
      try {
        requestTemplate.setSearchFromRegistrationDate(
           dateformat.parse(request.getParameter("fromRegistrationDate"))
        );
      } catch (Exception e) {
         requestTemplate.setSearchFromRegistrationDate(
            new Date()
         ); 
      }              
       
      try {
         requestTemplate.setSearchFromEndDate(
            dateformat.parse(request.getParameter("fromEndDate"))
         );
      } catch (Exception e) {
         requestTemplate.setSearchFromEndDate(
            new Date()
         );
      }
      
      return requestTemplate;
   }
   
    
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {                 
        RequestManagerLocalObject requestManager = 
          requestManagerHome.create();                                     
        
        Collection<RequestStatusBean> statusList = 
           requestManager.selectRequestStatuses();
        
        Date toRegistrationDate = new Date();
        Date toEndDate = new Date();
        Date fromRegistrationDate = new Date();
        Date fromEndDate = new Date();
        
        requestManager.remove();                                                               
                
        Action action = Action.valueOf(actionMapping.getActionName());
        
        switch (action) {
        case displayRequestSearchForm:
           Calendar calendar = Calendar.getInstance();
           toRegistrationDate = calendar.getTime();
           toEndDate = calendar.getTime();
           
           calendar.add(Calendar.MONTH, -1);
           
           fromRegistrationDate = calendar.getTime();
           fromEndDate = calendar.getTime();
        break;
        
        
        case processRequestSearchForm:
           RequestBean requestTemplate = 
              restoreBean(request);                      
              
           toRegistrationDate = 
              requestTemplate.getSearchToRegistrationDate();           
           toEndDate = 
              requestTemplate.getSearchToEndDate();            
           fromRegistrationDate = 
              requestTemplate.getSearchFromRegistrationDate();            
           fromEndDate = 
              requestTemplate.getSearchFromEndDate();
           
           request.getSession().setAttribute("searchRequestTemplate", requestTemplate);
           request.getSession().setAttribute("sidebarpage", 0);
           
           addInfMessage(request, config.getProperty("INF.SEARCH_COMPLEATED"));
        break;

        default:
           throw new RuntimeException("Unsupported action " + action);
        }
                
        request.setAttribute("statusList", statusList);
        
        request.setAttribute("toRegistrationDate", toRegistrationDate);                        
        request.setAttribute("toEndDate", toEndDate);                
        request.setAttribute("fromRegistrationDate", fromRegistrationDate);                        
        request.setAttribute("fromEndDate", fromEndDate);
                               
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}